package com.qf.dao.impl;

import com.qf.dao.CatalogDao;
import com.qf.pojo.Catalog;
import com.qf.utils.DBtools;
import com.qf.utils.SQL;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2021/10/19.
 */
public class CatalogDaoImpl implements CatalogDao {

    private QueryRunner qr = new QueryRunner(DBtools.getDataSource());

    @Override
    public List<Catalog> selectAllCatalog(int level, int param) {//3,5
        //level=1级/2级/3级
        //默认查询一级
        try {
            StringBuffer sql = new StringBuffer("SELECT " + SQL.CATALOG + " FROM catalog WHERE cat_level=?");
            if (level == 2) {
                //查询二级
                sql.append(" and cat_line=?");
                return qr.query(sql.toString(), new BeanListHandler<Catalog>(Catalog.class), level, param);
            } else if (level == 3) {
                //查询三级
                sql.append(" and cat_parent=?");
                return qr.query(sql.toString(), new BeanListHandler<Catalog>(Catalog.class), level, param);
            }
            return qr.query(sql.toString(), new BeanListHandler<Catalog>(Catalog.class), level);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
